package com.sjh.lib_app.utils

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import android.view.animation.*
import androidx.core.animation.addListener
import androidx.core.animation.doOnEnd


/**
 *  一些简单的动画工具
 * @author : SunJH
 * @data : 2020/9/1
 */
/**
 * 旋转动画
 * @param angle 旋转角度 默认旋转180度
 * @param anmianTime 动画持续时间
 */
fun View.rotationAnmian(angle: Int = -180, anmianTime: Long = 300) {
    this.animate().rotation(angle.toFloat())
        .apply { duration = anmianTime;interpolator = AccelerateDecelerateInterpolator() }.start()
}
/**
 * 带出入动画的显示和隐藏
 * @param visibility 显示或者隐藏，只支持 VISIBLE 和 GONE
 * @param animationTime 动画时长 默认300毫秒
 */
fun View.visibilityAnimation(visibility: Int, animationTime: Long = 300) {
    //获取坐标
    val location = IntArray(2)
    this.getLocationInWindow(location)
    val y = location[1] // view距离window 顶边的距离（即y轴方向）
    this.visibility = visibility

    if (visibility == View.VISIBLE) {
        //位移动画
        val translateAnimation = TranslateAnimation(0f, 0f, (0 - y * 2).toFloat(), 0f).apply {
            duration = animationTime
            fillAfter = true
        }

        val alphaAnimation = AlphaAnimation(1.0f, 1.0f).apply {
            duration = animationTime
            fillAfter = true
        }
        val animationSet = AnimationSet(true)
        animationSet.addAnimation(translateAnimation)
        animationSet.addAnimation(alphaAnimation)
        this.animation = animationSet
    } else {
        //位移动画
        val translateAnimation = TranslateAnimation(0f, 0f, 0f, (0 - y * 2).toFloat()).apply {
            duration = animationTime
            fillAfter = true
        }

        val alphaAnimation = AlphaAnimation(1.0f, 1.0f).apply {
            duration = animationTime
            fillAfter = true
        }
        val animationSet = AnimationSet(true)
        animationSet.addAnimation(translateAnimation)
        animationSet.addAnimation(alphaAnimation)
        this.animation = animationSet
    }
}

/**
 * 带渐隐动画的显示和隐藏操作
 */
fun View.showOrHidAlphaAnimation(visibility: Int, animationTime: Long = 200) {

    val animation = AlphaAnimation(if (visibility == View.VISIBLE) 0f else 1f, if (visibility == View.VISIBLE) 1f else 0f).apply {
        duration = animationTime
        setAnimationListener(object : SimpleAnimationUtils.AnimationListenerAdapter() {
            override fun onAnimationStart(animation: Animation?) {
                if (visibility == View.VISIBLE) {
                    this@showOrHidAlphaAnimation.visibility = visibility
                }
            }

            override fun onAnimationEnd(animation: Animation?) {
                super.onAnimationEnd(animation)
                if (visibility == View.GONE) {
                    this@showOrHidAlphaAnimation.visibility = visibility
                }
            }
        })
    }
    this.startAnimation(animation)
}

/**
 * 缩放XY方向
 */
fun View.scaleXY(from: Float, to: Float, duration: Long = 300, interpolator: Interpolator = LinearInterpolator()){
    val animatorX = ObjectAnimator.ofFloat(this, "scaleX", from, to)
    val animatorY = ObjectAnimator.ofFloat(this, "scaleY", from, to)
    val animatorSet = AnimatorSet()
    animatorSet.playTogether(animatorX, animatorY)
    animatorSet.duration = duration
    animatorSet.interpolator = interpolator
    animatorSet.start()
}

/**
 * 给控件设置无限旋转动画
 */
fun View.setInfiniteRotateAnimator():ObjectAnimator{
    val ofFloat = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f)
    val linearInterpolator = LinearInterpolator()
    ofFloat.repeatCount = Animation.INFINITE
    ofFloat.interpolator = linearInterpolator
    ofFloat.duration = 500
    ofFloat.start()
    return ofFloat
}

